import request from './axios';

interface IProductQuery {
  page?: number;
  size?: number;
  sort?: string;
  direction?: string;
  keyword?: string;
  name?: string;
  sku?: string;
  brand?: string;
  description?: string;
}

export interface IProductDto {
  id?: string;
  name: string;
  sku: string;
  description?: string;
  price: number;
  originalPrice?: number;
  stock?: number;
  categoryId: number;
  categoryName?: string;
  brand?: string;
  coverImage?: string;
  images?: IProductImageDto[];
  attributes?: IProductAttributeDto[];
  isPublished?: boolean;
  isRecommended?: boolean;
  isNew?: boolean;
  isHot?: boolean;
  sales?: number;
  createdAt?: string;
  updatedAt?: string;
}

export interface IProductImageDto {
  id?: number;
  productId?: string;
  imageUrl: string;
  sort?: number;
  main?: boolean;
}

export interface IProductAttributeDto {
  id?: number;
  productId?: string;
  name: string;
  value: string;
}

const productApi = {
  // 获取商品列表（分页）
  getProductsPage: (params: IProductQuery = {}) => {
    console.log('调用商品分页API，参数:', params);
    return request({
      url: 'product/page',
      method: 'get',
      params
    });
  },
  
  // 通过ID获取商品
  getProductById: (id: string) => {
    console.log('获取商品详情，ID:', id);
    return request({
      url: `product/${id}`,
      method: 'get'
    });
  },
  
  // 获取新品商品
  getNewProducts: (limit: number = 10) => {
    console.log('获取新品商品，数量限制:', limit);
    return request({
      url: 'product/new',
      method: 'get',
      params: { limit }
    });
  },
  
  // 获取推荐商品
  getRecommendedProducts: (limit: number = 10) => {
    console.log('获取推荐商品，数量限制:', limit);
    return request({
      url: 'product/recommended',
      method: 'get',
      params: { limit }
    });
  },
  
  // 获取热门商品
  getHotProducts: (limit: number = 10) => {
    console.log('获取热门商品，数量限制:', limit);
    return request({
      url: 'product/hot',
      method: 'get',
      params: { limit }
    });
  },
  
  // 搜索商品（快速搜索）
  searchProducts: (keyword: string, params: IProductQuery = {}) => {
    console.log('搜索商品，关键词:', keyword, '参数:', params);
    return request({
      url: 'product/search/quick',
      method: 'get',
      params: { ...params, keyword }
    });
  },
  
  // 使用Elasticsearch搜索商品
  searchByES: (keyword: string, page: number = 0, size: number = 10) => {
    console.log('使用ES搜索商品，关键词:', keyword, '页码:', page, '每页数量:', size);
    return request({
      url: 'product/es/search',
      method: 'get',
      params: { keyword, page, size }
    });
  },
  
  // 高级搜索
  advancedSearch: (params: IProductQuery) => {
    console.log('高级搜索，参数:', params);
    return request({
      url: 'product/search/advanced',
      method: 'get',
      params
    });
  },
  
  // 根据分类ID获取商品
  getProductsByCategory: (categoryId: number, pageNum: number = 1, pageSize: number = 10) => {
    console.log('根据分类获取商品，分类ID:', categoryId, '页码:', pageNum, '每页数量:', pageSize);
    return request({
      url: `product/category/${categoryId}`,
      method: 'get',
      params: { pageNum, pageSize }
    });
  },
  
  // 创建商品
  createProduct: (product: IProductDto) => {
    console.log('创建商品，数据:', product);
    return request({
      url: 'product',
      method: 'post',
      data: product
    });
  },
  
  // 更新商品
  updateProduct: (id: string, product: IProductDto) => {
    console.log('更新商品，ID:', id, '数据:', product);
    return request({
      url: `product/${id}`,
      method: 'put',
      data: product
    });
  },
  
  // 删除商品
  deleteProduct: (id: string) => {
    console.log('删除商品，ID:', id);
    return request({
      url: `product/${id}`,
      method: 'delete'
    });
  },
  
  // 发布/下架商品
  publishProduct: (id: string, isPublished: boolean) => {
    console.log('更新商品发布状态，ID:', id, '发布状态:', isPublished);
    return request({
      url: `product/${id}/publish`,
      method: 'put',
      params: { isPublished }
    });
  },
  

  // 获取商品图片
  getProductImages: (productId: string) => {
    return request({
      url: `product/${productId}/images`,
      method: 'get'
    });
  },
  
  // 添加商品图片
  addProductImage: (productId: string, image: IProductImageDto) => {
    return request({
      url: `product/${productId}/images`,
      method: 'post',
      data: image
    });
  },
  
  // 删除商品图片
  deleteProductImage: (imageId: number) => {
    return request({
      url: `product/images/${imageId}`,
      method: 'delete'
    });
  },
  
  // 上传图片
  uploadImage: (file: File) => {
    const formData = new FormData();
    formData.append('file', file);
    
    return request({
      url: 'product/images/upload',
      method: 'post',
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      data: formData
    });
  },
  
  // 上传多个图片
  uploadMultipleImages: (files: File[]) => {
    const formData = new FormData();
    files.forEach(file => {
      formData.append('files', file);
    });
    
    return request({
      url: 'product/images/upload/multiple',
      method: 'post',
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      data: formData
    });
  },
  
  // 上传并直接关联到商品
  uploadProductImage: (productId: string, file: File, isMain: boolean = false) => {
    const formData = new FormData();
    formData.append('file', file);
    formData.append('isMain', isMain.toString());
    
    return request({
      url: `product/${productId}/image-upload`,
      method: 'post',
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      data: formData
    });
  },
  
  // 批量上传并关联到商品
  uploadMultipleProductImages: (productId: string, files: File[]) => {
    const formData = new FormData();
    files.forEach(file => {
      formData.append('files', file);
    });
    
    return request({
      url: `product/${productId}/image-upload/batch`,
      method: 'post',
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      data: formData
    });
  },
  
  // 获取商品属性
  getProductAttributes: (productId: string) => {
    return request({
      url: `product/${productId}/attributes`,
      method: 'get'
    });
  },
  
  // 添加商品属性
  addProductAttribute: (productId: string, attribute: IProductAttributeDto) => {
    return request({
      url: `product/${productId}/attributes`,
      method: 'post',
      data: attribute
    });
  },
  
  // 删除商品属性
  deleteProductAttribute: (attributeId: number) => {
    return request({
      url: `product/attributes/${attributeId}`,
      method: 'delete'
    });
  }
};

export default productApi; 